package com.exwave.socialgame.model_websocket;

import com.exwave.socialgame.myutils.ReturnUtils;

import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController("Websocket")
@RequestMapping("/message")
public class WebSocketController {

    @GetMapping("/sent")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "目标用户UID", dataTypeClass = String.class, required = true, paramType = "query"),
            @ApiImplicitParam(name = "message", value = "发送的消息", dataTypeClass = String.class, required = true, paramType = "query")
    })
    @ApiOperation(value = "向目标用户发送信息")
    public Object sendTo(@Param("sid") String sid, @Param("message") String message) {
        if (WebSocketServer.sendMessageTo(sid, message))
            return ReturnUtils.returnMap(200, message);
        return ReturnUtils.returnMap(404, "用户不在线");
    }


    @GetMapping("/sentall")
    @ApiImplicitParam(name = "message", value = "发送的信息", required = true, dataTypeClass = String.class, paramType = "query")
    @ApiOperation(value = "向所有连接用户发送信息")
    public Object sendToAll(@Param("message") String message) {
        int all = WebSocketServer.sendMessageToAll(message);
        if (all != 0)
            return ReturnUtils.returnMap(200, "已发送给 " + all + " 个在线用户");
        return ReturnUtils.returnMap(404, "当前没有用户在线");
    }
}
